<?php
namespace app\admin\controller;

use \app\common\adapter\AuthAdapter;
use \think\facade\Config;
use \com\verify\HonrayVerify;

class Base extends \app\common\controller\Common {

	use \app\common\model\TreeBase;

	public function login(){
		$username=$this->param('user_name');
		$password=$this->param('password');
		$isRemember=$this->param('is_remember',false);
		$userModel=model('admin/AdminUser');
		if (intval(config('system.IDENTIFYING_CODE'))) {
			$v = new HonrayVerify(config('captcha.'));
			if (!$v->check($this->param('verify_code',''),$this->param('verify_key',''))) {
				return json_pack('登陆失败(验证码错误)', 400);
			}
		}

		if ($data=$userModel->login($username,$password,$isRemember)){
			return json_pack($data);
		} else {
			return json_pack("登陆失败(".$userModel->getError().")",400);
		}
	}

	public function relogin(){
		if (!($rememberKey=$this->param('rememberKey')))
			return json_pack("已登出");
		$secret=decrypt($rememberKey);
		$userModel=model('admin/AdminUser');

		if ($res=$userModel->login($secret['user_name'],$secret['password'],true)){
			return json_pack($res);
		} else {
			return json_pack("自动重登录失败",STATUS_ACCOUNT_DISABLE);
		}
	}

	public function logout(){
		$authKey=$this->request->header('authKey');
		cache('Auth_'.$authKey, null);
		return json_pack("登出成功");
	}

	public function register() {

	}

	public function getVerify() {
		$key = $this->param('key', null);
		if ($key === null) {
			return response('必须指定key',404);
		}
		$v=new HonrayVerify(config('captcha.'));
		return $v->entry($key);
	}

	public function getConfigs(){
		$systemConfig = cache('DB_CONFIG_DATA');
		if (!$systemConfig) {
			//获取所有系统配置
			$systemConfig = model('admin/SystemConfig')->getDataList();
			cache('DB_CONFIG_DATA', null);
			cache('DB_CONFIG_DATA', $systemConfig, 36000); //缓存配置
		}
		return json_pack($systemConfig);
	}

	// miss 路由：处理没有匹配到的路由规则
	public function miss()
	{
		if (Request::instance()->isOptions()) {
			return ;
		} else {
			echo 'vuethink接口';
		}
	}
}


?>